import { defineConfig, loadEnv } from 'vite'
import { createSvgIconsPlugin } from "vite-plugin-svg-icons";
import vueJsx from '@vitejs/plugin-vue-jsx'
import vue from '@vitejs/plugin-vue'
import { resolve } from 'path'

// 项目根路径
const root = process.cwd()
function pathResolve(dir: string) {
  return resolve(root, '.', dir)
}
// https://vite.dev/config/
export default defineConfig(
  ({ command, mode }) => {
    console.log(command, mode);
    let env = {} as any
    if (command === 'serve') {
      env = loadEnv("development", root)
    } else {
      env = loadEnv("production", root, '')
    }
    return {
      base: env.VITE_BASE_PATH,
      plugins: [
        vue(),
        vueJsx(),
        createSvgIconsPlugin({
          // 指定要缓存的图标目录
          iconDirs: [resolve(process.cwd(), "src/assets/icons")],
          // 指定symbolId格式
          symbolId: "icon-[dir]-[name]",
          svgoOptions: true
        })
      ],
      resolve: {
        extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.less', '.css'],
        alias: {
          "@": pathResolve('./src')
        },
      },
      server: {
        port: 3000,
        host: '0.0.0.0',
        hmr: true, // 热更新
      }
    }
  }
)
